﻿var CalculatorVisa = function ( ddlPartnerName, ddlVisaTypeName, ddlVisaName,
                                 txtPriceAdults,txtTranslateAdult,txtOtherCostAdult,
                                 txtPriceChild, txtTranslateChild, txtOtherCostChild,
                                 txtQuantityAdults, txtQuantityChild, txtVat,
                                 txtTotalPriceVisa, txtNoteVisa, seasonTour, tourInfo,
                                 txtPriceBabyVisa ,  txtTranslateBabyVisa ,  txtOtherCostBabyVisa ,  txtQuantityBabyVisa ) {
   
    $(txtPriceAdults).val(0); $(txtTranslateAdult).val(0); $(txtOtherCostAdult).val(0);
    $(txtPriceChild).val(0); $(txtTranslateChild).val(0); $(txtOtherCostChild).val(0);
    $(txtQuantityAdults).val(0); $(txtQuantityChild).val(0); $(txtVat).val(0);
    $(txtTotalPriceVisa).val(0);
    $(txtPriceBabyVisa).val(0); $(txtTranslateBabyVisa).val(0); $(txtOtherCostBabyVisa).val(0);
    $(txtQuantityBabyVisa).val(0);
    //load Visa
    var loadByVisa = function () {

        var typeId = 6;
        var partnerId = $(ddlPartnerName).val();
        var visaTypeId = $(ddlVisaTypeName).val();
        
        if (partnerId <= 0 || visaTypeId <= 0) {
            $(ddlVisaName).html('<option value="-1">--- Chọn Visa ---</option>'); return;
        }
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceListByVisa", "PartnerId": partnerId, "VisaTypeId": visaTypeId },
            dataType: "json",
            success: function (data) {
                $(ddlVisaName).html('<option value="-1">--- Chọn Visa ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlVisaName)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.PriceVisa));
                    });
                }
            }
        });
    };
    //load Visa
   
    // goi để load visa
    $(ddlPartnerName).change(loadByVisa);
    $(ddlVisaTypeName).change(loadByVisa);
    
    ////load Gia Visa
    var loadPricel = function () { 
        var menuElementId = $(ddlVisaName).val();
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceVisa", "PriceVisaId": menuElementId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    
                    $(txtPriceAdults).val(data.Price),
                    $(txtTranslateAdult).val(data.Stranlate),
                    $(txtOtherCostAdult).val(data.OtherCosts),
                    
                    $(txtPriceChild).val(data.Price),
                    $(txtTranslateChild).val(data.Stranlate),
                    $(txtOtherCostChild).val(data.OtherCosts),
                    
                    $(txtPriceBabyVisa).val(data.Price);
                    $(txtTranslateBabyVisa).val(data.Stranlate);
                    $(txtOtherCostBabyVisa).val(data.OtherCosts);
                
                    funTong();
                }
            }

        });
    };
    var funTong = function () {
      
        var priceAdults=     $(txtPriceAdults).val();
        var translateAdult = $(txtTranslateAdult).val();
        var otherCostAdult = $(txtOtherCostAdult).val();
        
        var priceChild =     $(txtPriceChild).val();
        var translateChild = $(txtTranslateChild).val();
        var otherCostChild = $(txtOtherCostChild).val();

        var priceBaby = $(txtPriceBabyVisa).val();
        var translateBaby = $(txtTranslateBabyVisa).val();
        var otherCostBaby = $(txtOtherCostBabyVisa).val();
        
        var quantityAdults = $(txtQuantityAdults).val();
        var quantityChild = $(txtQuantityChild).val();
        var quantityBaby = $(txtQuantityBabyVisa).val();
        
        var vat = $(txtVat).val();
      
        var total = 0;
        if (vat > 0 || vat < 100) {
            total = (((parseInt(priceAdults) + parseInt(translateAdult) + parseInt(otherCostAdult)) * parseInt(quantityAdults)) +
                       ((parseInt(priceChild) + parseInt(translateChild) + parseInt(otherCostChild)) * parseInt(quantityChild)) +
                       ((parseInt(priceBaby) + parseInt(translateBaby) + parseInt(otherCostBaby)) * parseInt(quantityBaby))
            );
            total = total + (total * vat / 100);
        }
        $(txtTotalPriceVisa).val(total);
    };

    // goi load gia
    $(ddlVisaName).change(loadPricel);
    $(txtPriceAdults).keyup(funTong);
    $(txtTranslateAdult).keyup(funTong);
    $(txtOtherCostAdult).keyup(funTong);
    $(txtPriceChild).keyup(funTong);
    $(txtTranslateChild).keyup(funTong);
    $(txtOtherCostChild).keyup(funTong);
    $(txtQuantityAdults).keyup(funTong);
    $(txtQuantityChild).keyup(funTong);
    $(txtVat).keyup(funTong);
    $(txtPriceBabyVisa).keyup(funTong);
    $(txtTranslateBabyVisa).keyup(funTong);
    $(txtOtherCostBabyVisa).keyup(funTong);
    $(txtQuantityBabyVisa).keyup(funTong);
    var funcVisa = function () {
        
        var typeId = 6;
        var visaId = $(ddlVisaName).val();
        var priceAdults = $(txtPriceAdults).val();
        var translateAdult = $(txtTranslateAdult).val();
        var otherCostAdult = $(txtOtherCostAdult).val();

        var priceChild = $(txtPriceChild).val();
        var translateChild = $(txtTranslateChild).val();
        var otherCostChild = $(txtOtherCostChild).val();

        var priceBaby = $(txtPriceBabyVisa).val();
        var translateBaby = $(txtTranslateBabyVisa).val();
        var otherCostBaby = $(txtOtherCostBabyVisa).val();

        var quantityAdults = $(txtQuantityAdults).val();
        var quantityChild = $(txtQuantityChild).val();
        var quantityBaby = $(txtQuantityBabyVisa).val();
        var vat = $(txtVat).val();
        var tourId = tourInfo;
        if (vat > 0 && vat <= 100) {
        } else {
            vat = 0;
        }
        var calculatorId = $(this).attr("calculatorid");

        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: {
                "function": "InsertVisaItem", "TypeId": typeId, "TourId": tourId, "ItemId": visaId,
                "CalculatorId": calculatorId,"ClassName": className,'Vat': vat,
                "PriceAdults": priceAdults,"PriceChilds": priceChild,"RriceBaby": priceBaby,
                'TranslateAdult': translateAdult,'TranslateChild': translateChild,'TranslateBaby': translateBaby,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild, 'QuantityBaby': quantityBaby,
                'OtherCostAdult': otherCostAdult,'OtherCostChild': otherCostChild,'QtherCostBaby': otherCostBaby
            },
            dataType: "json",
            success: function(data) {
                $(".ListVisa").remove();

                $(ddlVisaName).val('-1');
                $(ddlPartnerName).val("-1");
                $(ddlVisaTypeName).val("-1");
                $(txtPriceAdults).val("0");
                $(txtTranslateAdult).val("0");
                $(txtOtherCostAdult).val("0");
                $(txtPriceChild).val("0");
                $(txtTranslateChild).val("0");
                $(txtOtherCostChild).val("0");
                $(txtQuantityAdults).val("0");
                $(txtQuantityChild).val("0");
                $(txtVat).val("0");
                $(txtTotalPriceVisa).val("0");
                $(txtPriceBabyVisa).val("0");
                $(txtTranslateBabyVisa).val("0");
                $(txtOtherCostBabyVisa).val("0");
                $(txtQuantityBabyVisa).val("0");
                if (data && data.isError == false) {
                    $.each(data.array, function(index, item) {

                        var totaIitem = ((parseInt(item.PriceAdults) + parseInt(item.TranslateAdult) + parseInt(item.OtherCostAdult)) * parseInt(item.QuantityAdults)) +
                            ((parseInt(item.PriceChild) + parseInt(item.TranslateChild) + parseInt(item.OtherCostChild)) * parseInt(item.QuantityChild)) +
                            ((parseInt(item.RriceBaby) + parseInt(item.TranslateBaby) + parseInt(item.OtherCostBaby)) * parseInt(item.QuantityBaby));
                        totaIitem = totaIitem + (totaIitem * item.Vat / 100);
                        $('#new-Visa')
                            .before($("<tr class='ListVisa' id='Visa-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.VisaTypeName + '</td>' +
                                '<td >' + item.VisaName + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.TranslateAdult + '</td>' +
                                '<td >' + item.OtherCostAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.TranslateChild + '</td>' +
                                '<td >' + item.OtherCostChild + '</td>' +
                                '<td >' + item.RriceBaby + '</td>' +
                                '<td >' + item.TranslateBaby + '</td>' +
                                '<td >' + item.OtherCostBaby + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td >' + totaIitem + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateVisa"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteVisa"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalVisaPrices').html(data.TotalVisaPrices);
                    $('#TotalVisaAdults').html(data.TotalVisaAdults);
                    $('#TotalVisaChild').html(data.TotalVisaChild);
                    $('#TotalVisaBaby').html(data.TotalVisaBaby);

                    $('#btnInsertVisa').attr("calculatorid", "-1").val("Thêm");
                    $('.btnDeleteVisa').show();
                    $('#btnDeleteVisa').hide();
                }

            }
        });
    };
    $('#btnInsertVisa').live("click", funcVisa); 
    $('.btnDeleteVisa').live("click",  funcVisa);
    $('.btnUpdateVisa').live("click", function () {
        var calculatorId = $(this).attr("calculatorid");
        var callAjax = $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "UpdateVisa", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) { 
                $('#Visa-' + calculatorId).remove(); 
                $(ddlPartnerName).val(data.PartnerId);
                $(ddlVisaTypeName).val(data.VisaTypeId);
              
            
                loadVisa(ddlVisaName, data.PartnerId, data.VisaTypeId, data.VisaPriceId);
                $(txtPriceAdults).val(data.PriceAdults);
                $(txtPriceChild).val(data.PriceChild);
                $(txtPriceBabyVisa).val(data.RriceBaby);
                
                $(txtOtherCostAdult).val(data.OtherCostAdult);
                $(txtOtherCostChild).val(data.OtherCostChild);
                $(txtOtherCostBabyVisa).val(data.QtherCostBaby);
                
                $(txtTranslateAdult).val(data.TranslateAdult);
                $(txtTranslateChild).val(data.TranslateChild);
                $(txtTranslateBabyVisa).val(data.TranslateBaby);
                
                $(txtQuantityAdults).val(data.QuantityAdults);
                $(txtQuantityChild).val(data.QuantityChild);
                $(txtQuantityBabyVisa).val(data.QuantityBaby);
                
                $(txtVat).val(data.VAT);
                $(txtTotalPriceVisa).val(data.TotalPriceVisa); 
                $('#btnInsertVisa').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateVisa').hide();
                $('#btnDeleteVisa').show();
            }
        });
    });
};

